﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IteratorPattern.Practical.IntrinsicIterator
{
    /// <summary>
    /// 定義一個具體的聚合類
    /// </summary>
    public class ConcreteAggregate : Aggregate
    {
        //存儲元素的集合
        private static object[] objs = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };


        /// <summary>
        /// 定義具體的迭代器類
        /// </summary>
        private class ConcreteIterator : Iterator
        {
            private int index = 0;
             
            /// <summary>
            /// 獲取當前遍曆的元素
            /// </summary>
            public object CurrentItem()
            {
                return objs[index];

            }

            public void First()
            {
                index = 0;
            }

            /// <summary>
            /// 判斷是否遍曆完畢
            /// </summary>
            /// <returns></returns>
            public bool IsDone()
            {
                return index >= objs.Length;
            }

            public void Next()
            {
                if (index < objs.Length)
                {
                    index++;
                }
            }
        }

        /// <summary>
        /// 獲取元素個數
        /// </summary>
        /// <returns></returns>
        public int Size()
        {
            return objs.Length;
        }

        /// <summary>
        /// 獲取指定序號的元素
        /// </summary>
        /// <param name="index">指定的序號</param>
        /// <returns></returns>
        public object GetElement(int index)
        {
            if (index < 0 || index > objs.Length)
            {
                return null;
            }

            return objs[index];
        }

        /// <summary>
        /// 創建該聚合類的迭代器
        /// </summary>
        /// <returns></returns>
        public override Iterator CreateIterator()
        {
            return new ConcreteIterator();
        }
    }
}
